module TestGame;

import std.stdio;
import std.math;
import std.math2;

import derelict.opengl.gl;
import derelict.opengl.gltypes;
import derelict.opengl.glu;
import derelict.sdl.sdl;
import derelict.util.loader;

import dgl.Event;
import dgl.Game;
import dgl.GameTime;
import dgl.Vector2;
import dgl.Vector3;
import dgl.Vector4;
import dgl.Rectangle;
import dgl.Matrix;

import dgl.input.Mouse;
import dgl.input.MouseState;
import dgl.input.MouseEventArgs;

import dgl.graphics.Model;
import dgl.graphics.ModelMesh;
import dgl.graphics.VertexData;
import dgl.graphics.Color;
import dgl.graphics.FaceData;
import dgl.graphics.Texture2D;
import dgl.graphics.TexSeqPlayer;

class TestGame : Game
{
	Model model;

	Vector3 cameraPos;

	Vector3 lookingPt;
	
	Texture2D texture;
	
	Vector2 texPos;
	
	float texRotation;
	
	uint texAlpha;
	
	TexSeqPlayer player;

	public this()
	{
		super();
		cameraPos = Vector3( 10, 0, 10 );
		lookingPt = Vector3.zero;
		this.window.clientBounds = Rectangle( 0, 0, 1024, 768 );
		this.window.title = "Fuck!";
		texPos = Vector2( 100, 100 );
		texAlpha = 255;
		texRotation = 0;
	}

	protected void initialize()
	{
		super.initialize();
		this.cursorDisplay = false;
		Mouse.leftButtonDown ~= &leftDown;
		Mouse.wheelUp ~= &wheelUp;
	}
	
	private void leftDown( Object sender, MouseEventArgs args )
	{
		writefln( args.mouseState.x );
		writefln( args.mouseState.y );
	}
	
	private void wheelUp( Object sender, MouseEventArgs args )
	{
		writefln( args.mouseState.scrollWheelValue );
	}

	protected void loadContent()
	{
		model = Model.loadFromFile( "Content\\Model\\testvcolor.dmo" );
		texture = Texture2D.loadFromFile( "Content\\Texture\\TestSpriteBatch.png" );
		Texture2D textureSeq = Texture2D.loadFromFile( "Content\\Texture\\flame.png" );
		player = new TexSeqPlayer( this.spriteBatch,
			   					   textureSeq,
			   					   Vector2( 64, 64 ),
			   					   Vector2( 200, 200 ),
			   					   1.2,
			   					   ArrangeMode.Horizontal,
			   					   true );
		player.play();
	}

	protected void unloadContent()
	{

	}

	protected void update( GameTime gameTime )
	{
		player.update( gameTime );
		
		Uint8 * keys = SDL_GetKeyState( null );
		if( keys[SDLK_w] )
		{
			cameraPos.x = cameraPos.x - 0.01;
			//lookingPt.X = lookingPt.X - 0.2;
			//texPos.y = texPos.y - 1;
		}
		if( keys[SDLK_s] )
		{
			cameraPos.x = cameraPos.x + 0.01;
			//lookingPt.X = lookingPt.X + 0.2;
			//texPos.y = texPos.y + 1;
		}
		if( keys[SDLK_a] )
		{
			cameraPos.z = cameraPos.z + 0.01;
			//lookingPt.Z = lookingPt.Z + 0.2;
			//texPos.x = texPos.x - 1;
		}
		if( keys[SDLK_d] )
		{
			cameraPos.z = cameraPos.z - 0.01;
			//lookingPt.Z = lookingPt.Z - 0.2;
			//texPos.x = texPos.x + 1;
		}
		if( keys[SDLK_UP] )
		{
			cameraPos.y = cameraPos.y + 0.01;
			//lookingPt.Y = lookingPt.Y + 0.2;
			//texAlpha += 1;
			//if( texAlpha >= 255 )
			//	texAlpha = 255;
		}
		if( keys[SDLK_DOWN] )
		{
			cameraPos.y = cameraPos.y - 0.01;
			//lookingPt.Y = lookingPt.Y - 0.2;
			//if( texAlpha > 0 )
			//	texAlpha -= 1;
			//if( texAlpha <= 0 )
			//	texAlpha = 0;
		}
		
		texPos.x = Mouse.getState().x;
		texPos.y = Mouse.getState().y;
		
		texAlpha = Mouse.getState().x * Mouse.getState().y * 255 / 1024 / 768;
		
		texRotation = gameTime.totalGameTime * 3.14;
	}

	protected void draw( GameTime gameTime )
	{
		glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
		glEnable( GL_DEPTH_TEST );
		
		foreach( meshContainer; model.meshContainers )
		{
			meshContainer.matView = Matrix.createLookAt( cameraPos, Vector3.zero, Vector3.unitZ );
			meshContainer.matProj = Matrix.createPerspectiveFieldOfView( 3.14f / 4.0f, 1024.0f / 768.0f, 1, 10000 );
			meshContainer.draw();
		}
		
		this.spriteBatch.begin();
		this.spriteBatch.draw( texture, Vector2( 220, 220 ), 0.5 );
		this.spriteBatch.end();
		
		player.draw( gameTime );
		
	}
}